package tuiterbot

import static org.junit.Assert.*
import org.junit.*

class DatabaseTests {
    Admin existingAdmin
    Admin existingAdmin2
    Account existingAccount
    Account existingAccount2
    UserAccount existingUserAccount
    UserAccount existingUserAccount2
    AutoSuggestion existingAutoSuggestion
    Suggestion existingSuggestion
    PendingRT existingPendingRT

    @Before
    void setUp() {
	// Se insertan una serie de entidades en la base de datos
	// persistir administrador
	existingAdmin = new Admin(screenName: "adminAccount",
	password: "jjjjj", mail: "mail@gmail.com")
	assertNotNull existingAdmin.save(flush:true)
	existingAdmin2 = new Admin(screenName: "adminAccount2",
	password: "jjjjj", mail: "mail2@gmail.com")
	assertNotNull existingAdmin2.save(flush:true)
	existingAccount = new Account(id: 0, screenName: "cuentaprueba",
	oauthToken: "esteesunoauthtokendeprueba",
	oauthTokenSecret: "esteesunoauthtokensecretdeprueba",
	profileImg: "urlImg" ,
	twitterId: 111111L,
	batchTime: 15,
	admin: existingAdmin,
	lastSinceDateDM:1328207838749)
	assertNotNull existingAccount.save(flush:true)
	existingAccount2 = new Account(id: 0, screenName: "cuentaprueba2",
	oauthToken: "esteesunoauthtokendeprueba2",
	oauthTokenSecret: "esteesunoauthtokensecretdeprueba2",
	profileImg: "urlImg2" ,
	twitterId: 111112L,
	batchTime: 15,
	admin: existingAdmin2,
	lastSinceDateDM:1328207838749)
	assertNotNull existingAccount2.save(flush:true)
	existingUserAccount = new UserAccount(id: 0, account: existingAccount,
	user: "user1",
	flag: false)
	if(existingUserAccount.save()) {
	}
	else {
	    existingUserAccount.errors.allErrors.each {
		println it

	    }
	}
	    assertNotNull existingUserAccount.save(flush:true)

	    existingUserAccount2 = new UserAccount(id: 0, account: existingAccount2,
	    user: "user1",
	    flag: false)
	    assertNotNull existingUserAccount2.save(flush:true)
	    
	    existingAutoSuggestion = new AutoSuggestion(hashtag: "#groovy",
	    userAccount: existingUserAccount, lastUpdated: 1328207838778, frequency: 1)
	    assertNotNull existingAutoSuggestion.save(flush:true)
	    
	    existingSuggestion = new Suggestion(hashtag: "#groovy", user: "userSugerido",
	    userAccount: existingUserAccount, lastUpdated: 1328207838778, frequency: 1)
	    assertNotNull existingSuggestion.save(flush:true)
	    existingPendingRT = new PendingRT(tweetId: 1000, tweetTimestamp: 1328200082208,
	    account: existingAccount)
	    assertNotNull existingPendingRT.save(flush:true)
	}

	@After
	void tearDown() {
	    // Tear down logic here
	}

	@Test
	void testDatabaseInsertions() {
	    /*
	     * Insertados...
	     * Admin: adminAccount, mail.@gmail.com
	     * Admin: adminAccount2, mail2.gmail.com
	     * Account: cuentaPrueba con administrador adminAccount
	     * Account: cuentaPrueba2 con administrador adminAccount2
	     * UserAccount: [cuentaPrueba, user1]
	     * UserAccount: [cuentaPrueba2, user1]
	     * AutoSuggestion: [#groovy, [user1, cuentaPrueba]]
	     * Suggestion: [#groovy, userSugerido, [cuentaPrueba, user1]]
	     * PendingRT: [cuentaPrueba, 1000]
	     */
	    // se intenta insertar un administrador con identificador duplicado
	    def aux = new Admin(screenName: "adminAccount",
	    password: "jjjjj", mail: "mail@gmail.com")
	    assertNull aux.save(flush:true)
	    // se intenta insertar una cuenta robot con identificador duplicado
	    aux = new Account(id: 0, screenName: "cuentaprueba",
	    oauthToken: "esteesunoauthtokendeprueba",
	    oauthTokenSecret: "esteesunoauthtokensecretdeprueba",
	    profileImg: "urlImg" ,
	    twitterId: 111111L,
	    batchTime: 15,
	    admin: existingAdmin,
	    lastSinceDateDM:1328207838749)
	    assertNull aux.save()
	    // se intenta insertar una cuenta robot asignada al mismo administrador
	    aux = new Account(id: 0, screenName: "cuentaprueba3",
	    oauthToken: "esteesunoauthtokendeprueba3",
	    oauthTokenSecret: "esteesunoauthtokensecretdeprueba3",
	    profileImg: "urlImg3" ,
	    twitterId: 111113L,
	    batchTime: 15,
	    admin: existingAdmin,
	    lastSinceDateDM:1328207838748)
	    assertNull aux.save()
	    // se intenta insertar userAccount duplicado
	    aux = new UserAccount(id: 0, account: existingAccount,
	    user: "user1",
	    flag: false)
	    assertNull aux.save(flush:true)

	    aux = new UserAccount(id: 0, account: existingAccount,
	    user: "user2",
	    flag: false)
	    assertNotNull aux.save(flush:true)
	    // se intenta insertar userAccount ok
	    aux = new UserAccount(id: 0, account: existingAccount2,
	    user: "user1",
	    flag: false)
	    assertNull aux.save(flush:true)

	    aux = new AutoSuggestion(hashtag: "#grails",
	    userAccount: existingUserAccount, lastUpdated: 1328207838778, frequency: 1)
	    assertNotNull aux.save(flush:true)

	    aux = new AutoSuggestion(hashtag: "#groovy",
	    userAccount: existingUserAccount, lastUpdated: 1328207838778, frequency: 1)
	    assertNull aux.save(flush:true)

	    aux = new AutoSuggestion(hashtag: "#groovy",
	    userAccount: existingUserAccount2, lastUpdated: 1328207838778, frequency: 1)
	    assertNotNull aux.save(flush:true)

	    aux = new Suggestion(hashtag: "#grails", user: "userSugerido",
	    userAccount: existingUserAccount, lastUpdated: 1328207838778, frequency: 1)
	    assertNotNull aux.save(flush:true)

	    aux = new Suggestion(hashtag: "#groovy", user: "userSugerido",
	    userAccount: existingUserAccount, lastUpdated: 1328207838778, frequency: 1)
	    assertNull aux.save(flush:true)

	    aux = new Suggestion(hashtag: "#groovy", user: "userSugerido",
	    userAccount: existingUserAccount2, lastUpdated: 1328207838778, frequency: 1)
	    assertNotNull aux.save(flush:true)

	    aux = new Suggestion(hashtag: "#grails", user: "userSugerido",
	    userAccount: existingUserAccount2, lastUpdated: 1328207838778, frequency: 1)
	    assertNotNull aux.save(flush:true)

	    aux = new PendingRT(tweetId: 1001, tweetTimestamp: 1328200082208,
	    account: existingAccount)
	    assertNotNull aux.save(flush:true)

	    aux = new PendingRT(tweetId: 1000, tweetTimestamp: 1328200082208,
	    account: existingAccount2)
	    assertNotNull aux.save(flush:true)
	    
	    assertTrue PendingRT.count() == 3
	    assertTrue Suggestion.count() == 4
	    assertTrue AutoSuggestion.count() == 3
	    assertTrue Admin.count() == 2
	    assertTrue Account.count() == 2
	    assertTrue UserAccount.count() == 3
	    
	    // probamos borrado
	    
	    //probamos consultas
	}
    }
